package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.business.interfaces.warningnews.OmsOrderWarningNewsBService;
import com.zmn.oms.common.constant.MessageQueueTagConsts;
import com.zmn.oms.model.entity.work.OrderWork;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 类描述：预警消息通知
 *
 * @author yangshuangping
 * @since 2022/06/22 16:09
 */
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class OmsOrderWarningNewsListener {
    @Resource
    private OmsOrderWarningNewsBService omsOrderWarningNewsBService;

    @AliyunMQHandler(tags = MessageQueueTagConsts.ORDER_WORK_TIME_OUT_NOTICE_TAG)
    public void orderWorkTimeOutNoticeTag(String message) {
        log.info("omsOrderWarningNewsListener:order_work_time_out_notice_tag:message={}", message);
        // 获取消息参数
        JSONObject jsonObject = JSON.parseObject(message);
        OrderWork oldOrderWork = jsonObject.getObject("orderWork", OrderWork.class);
        Integer timeoutType = jsonObject.getInteger("timeoutType");
        if (Objects.nonNull(oldOrderWork) || Objects.nonNull(timeoutType)) {
            // 机器人通知
            omsOrderWarningNewsBService.timeOutNoticeRobotNotice(oldOrderWork, timeoutType);
        } else {
            log.error("OmsOrderReleaseStockListener:order_work_time_out_notice_tag:error: 工单ID,超时类型为空");
        }
    }

    @AliyunMQHandler(tags = MessageQueueTagConsts.ORDER_WORK_WARNING_NOTICE_TAG)
    public void orderWorkWarningNoticeTag(String message) {
        log.info("omsOrderWarningNewsListener:order_work_warning_notice_tag:message={}", message);
        // 获取消息参数
        JSONObject jsonObject = JSON.parseObject(message);
        OrderWork oldOrderWork = jsonObject.getObject("orderWork", OrderWork.class);
        Integer timeoutType = jsonObject.getInteger("timeoutType");
        if (Objects.nonNull(oldOrderWork) || Objects.nonNull(timeoutType)) {
            // 机器人通知
            omsOrderWarningNewsBService.warningNoticeRobotNotice(oldOrderWork, timeoutType);
        } else {
            log.error("OmsOrderReleaseStockListener:order_work_warning_notice_tag:error: 订单ID,预警类型为空");
        }
    }
}
